package com.itic.mall.mobile.project;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.type.DefaultSysSettingKeyValue;
import com.itic.appbase.framework.type.UserShareType;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.mobile.share.LinkVo;
import com.itic.mall.system.project.version.persistence.model.Version;
import com.itic.mall.system.project.version.persistence.model.VersionExample;
import com.itic.mall.system.project.version.persistence.model.VersionExample.Criteria;
import com.itic.mall.system.project.version.service.VersionService;
import com.itic.mall.system.project.welcome.persistence.model.ProjectWelcome;
import com.itic.mall.system.project.welcome.persistence.model.ProjectWelcomeExample;
import com.itic.mall.system.project.loginpage.service.ProjectLoginpageService;
import com.itic.mall.system.project.loginpage.persistence.model.ProjectLoginpage;
import com.itic.mall.system.project.loginpage.persistence.model.ProjectLoginpageExample;
import com.itic.mall.system.project.welcome.service.ProjectWelcomeService;
import com.itic.mall.system.users.shareLog.service.UsersShareLogService;
import com.itic.mall.system.users.usersmall.persistence.model.UsersMall;



/**
 * 首页模块接口信息
 * @author 许小宝
 * @time 2017/12/01
 */
@Controller
@RequestMapping(value="/mobile/project")
public class ProjectInterface
     extends BaseMobileInterface {
	
	@Autowired
	private VersionService versionService;
	@Autowired
	private ProjectLoginpageService projectLoginpageService;
	@Autowired
	private ProjectWelcomeService projectWelcomeService;
	@Autowired
	private UsersShareLogService usersShareLogService;
	
	 // 返回属性黑名单
    private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "updateTime", "orgCode" };
          
    
      /**
	    * APP分享
	    * @return
	    */
	   @RequestMapping(value="/shareApp")
	   @ResponseBody
	   public Map<String,Object> shareApp( HttpServletRequest request,HttpServletResponse response,String token, String appId ){
		    Map<String, Object> resultMap = new HashMap<String,Object>();
		   	String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		String appDownloadUrl = "http://wap.uhuijia.com.cn/index.html#/invitedownload";
		   		Map<String, Object> dataMap = new HashMap<String,Object>();
		   		dataMap.put("poster", "http://file.jhjvip.cn/yhjlogo.png");
		   		//链接分享
				LinkVo urlVo = new LinkVo();
				urlVo.setImgUrl("http://file.jhjvip.cn/yhjlogo.png");
				urlVo.setTitle("点击领取京东内部大额友惠劵");
				if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId) ){
					UsersMall user = getUsersByTokenAndAppId(token, appId);
					user.getUserNickName();
					urlVo.setContent("你的好友"+user.getUserNickName()+"悄悄给你分享京东优惠劵了，点击查看~~" );
					this.usersShareLogService.addShareLog(UserShareType.App,null, user);
				}else{
					urlVo.setContent("你的好友悄悄给你分享京东优惠劵了，点击查看~~" );
				}
				urlVo.setUrl(appDownloadUrl);
				dataMap.put("link", urlVo);
		   		resultMap.put(DATA, dataMap); 
		   		
		   		errorCode = SUCCESS_ERROR_CODE;
		       	errorMsg = SUCCESS_ERROR_MSG;
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("ProjectInterface", "loginpage", e.toString(),"APP版本管理",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
		   	isCrrossDomain(response);
		   	return resultMap;
	   }
    
	   /**
	    * APP最新的版本信息
	    * @return
	    */
	   @RequestMapping(value="/version")
	   @ResponseBody
	   public Map<String,Object> version( HttpServletRequest request,HttpServletResponse response){
		    Map<String, Object> resultMap = new HashMap<String,Object>();
		   	String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		VersionVo version = new VersionVo();
		   		
		   	    VersionExample example = new VersionExample();
		   	    example.setOrderByClause("CREATE_TIME DESC");
		   	    Criteria criteria = example.createCriteria();
		   	    criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   	    criteria.andTypeEqualTo("android");
		   	    List<Version> list = versionService.list(example);
		   	    if(list.size()>0){
		   	    	version.setImage(list.get(0).getImg());
			   		version.setVersion(list.get(0).getVersion());//默认版本
			   		version.setDownload(list.get(0).getAppstoreDown());//默认版本
			   		version.setDesc(list.get(0).getVersionDesc());//默认版本
		   	    }else{
		   	    	version.setImage("http://file.jhjvip.cn/images/version.png");
			   		version.setVersion("1.0.0");//默认版本
			   		version.setDownload("http://wap.uhuijia.com.cn/index.html#/invitedownload");//默认版本
			   		version.setDesc("版本更新");//默认版本
		   	    }
		   	    
		   		resultMap.put(DATA, version);
		   		errorCode = SUCCESS_ERROR_CODE;
		       	errorMsg = SUCCESS_ERROR_MSG;
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("ProjectInterface", "loginpage", e.toString(),"APP版本管理",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
		   	isCrrossDomain(response);
		   	return resultMap;
	   }
	   
	   /**
	    * APP最新的版本信息
	    * @return
	    */
	   @RequestMapping(value="/versionIos")
	   @ResponseBody
	   public Map<String,Object> versionIos( HttpServletRequest request,HttpServletResponse response){
		    Map<String, Object> resultMap = new HashMap<String,Object>();
		   	String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		VersionVo version = new VersionVo();
		   		
		   		VersionExample example = new VersionExample();
		   	    example.setOrderByClause("CREATE_TIME DESC");
		   	    Criteria criteria = example.createCriteria();
		   	    criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   	    criteria.andTypeEqualTo("ios");
		   	    List<Version> list = versionService.list(example);
		   	    if(list.size()>0){
		   	    	version.setImage(list.get(0).getImg());
			   		version.setVersion(list.get(0).getVersion());//默认版本
			   		version.setDownload(list.get(0).getAppstoreDown());//默认版本
			   		version.setDesc(list.get(0).getVersionDesc());//默认版本
		   	    }else{
		   	    	version.setImage("http://file.jhjvip.cn/images/version.png");
			   		version.setVersion("1.0.0");//默认版本
			   		version.setDownload("http://wap.uhuijia.com.cn/index.html#/invitedownload");//默认版本
			   		version.setDesc("版本更新");//默认版本
		   	    }
		   		resultMap.put(DATA, version); 
		   		errorCode = SUCCESS_ERROR_CODE;
		       	errorMsg = SUCCESS_ERROR_MSG;
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("ProjectInterface", "loginpage", e.toString(),"APP版本管理",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
		   	isCrrossDomain(response);
		   	return resultMap;
	   }
	   /**
	    * 登录页图片获取接口
	    * @param request
	    * @param response
	    * @return
	    */
	   @RequestMapping(value="/loginpage")
	   @ResponseBody
	   public Map<String,Object> loginpage( HttpServletRequest request,HttpServletResponse response){
		    Map<String, Object> resultMap = new HashMap<String,Object>();
		   	String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		ProjectLoginpageExample example = new ProjectLoginpageExample();
		   		com.itic.mall.system.project.loginpage.persistence.model.ProjectLoginpageExample.Criteria criteria = example.createCriteria();
		   		Date date = new Date();
		   		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   		criteria.andStarttimeLessThan(date);
		   		criteria.andEndtimeGreaterThan(date);
		   		criteria.andTypeEqualTo("001");//注册页面用图片
		   		List<ProjectLoginpage> list = projectLoginpageService.list(example);
		   		if(list.size()>0){
			       	resultMap.put(DATA, ValueHelper.tranList2MapList(list, ignoreProperties));
			   		errorCode = SUCCESS_ERROR_CODE;
			       	errorMsg = SUCCESS_ERROR_MSG;
		   		}else{
		   			errorCode = PROJECT_LOGIN_PAGE_ERROR_CODE;
		   			errorMsg = PROJECT_LOGIN_PAGE_ERROR_MSG;
		   		}
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("ProjectInterface", "loginpage", e.toString(),"登录页图片获取接口",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
		   	isCrrossDomain(response);
		   	return resultMap;
	   }
	   
	   /**
	    * 注册页图片获取接口
	    * @param request
	    * @param response
	    * @return
	    */
	   @RequestMapping(value="/registpage")
	   @ResponseBody
	   public Map<String,Object> registpage( HttpServletRequest request,HttpServletResponse response){
		    Map<String, Object> resultMap = new HashMap<String,Object>();
		   	String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		ProjectLoginpageExample example = new ProjectLoginpageExample();
		   		com.itic.mall.system.project.loginpage.persistence.model.ProjectLoginpageExample.Criteria criteria = example.createCriteria();
		   		Date date = new Date();
		   		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   		criteria.andStarttimeLessThan(date);
		   		criteria.andEndtimeGreaterThan(date);
		   		criteria.andTypeEqualTo("002");//注册页面用图片
		   		List<ProjectLoginpage> list = projectLoginpageService.list(example);
		   		if(list.size()>0){
			       	resultMap.put(DATA, ValueHelper.tranList2MapList(list, ignoreProperties));
			   		errorCode = SUCCESS_ERROR_CODE;
			       	errorMsg = SUCCESS_ERROR_MSG;
		   		}else{
		   			errorCode = PROJECT_LOGIN_PAGE_ERROR_CODE;
		   			errorMsg = PROJECT_LOGIN_PAGE_ERROR_MSG;
		   		}
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("ProjectInterface", "registpage", e.toString(),"注册页图片获取接口",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
		   	isCrrossDomain(response);
		   	return resultMap;
	   }
	   /**
	    * 欢迎页
	    * @param request
	    * @param response
	    * @return
	    */
	    @RequestMapping(value = "/welcome")
		@ResponseBody
		public Map<String, Object> welcome(HttpServletRequest request, HttpServletResponse response) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		Date date = new Date();
		   		ProjectWelcomeExample example = new ProjectWelcomeExample();
		   		com.itic.mall.system.project.welcome.persistence.model.ProjectWelcomeExample.Criteria criteria = example.createCriteria();
		   		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   		criteria.andStartTimeLessThan(date);
		   		criteria.andEndTimeGreaterThan(date);
		   		criteria.andIsVisibleEqualTo(DBConst.TRUE);
		   		List<ProjectWelcome> list = projectWelcomeService.list(example);
		   		if(list.size()>0){
			       	resultMap.put("image",list.get(0).getImg());
			       	resultMap.put("isShow","hide");
			       	resultMap.put("url","http://develop.jhjvip.cn/h5/page/newyear.html");
			   		errorCode = SUCCESS_ERROR_CODE;
			       	errorMsg = SUCCESS_ERROR_MSG;
		   		}else{
		   			errorCode = INDEXPAGE_ERROR_CODE;
		   			errorMsg = INDEXPAGE_ERROR_MSG;
		   		}
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("IndexPageInterface", "index", e.toString(),"APP欢迎页",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
			return resultMap;
		}
	    
	   /**
	    * 欢迎页
	    * @param request
	    * @param response
	    * @return
	    */
	    @RequestMapping(value = "/about")
		@ResponseBody
		public Map<String, Object> about(HttpServletRequest request, HttpServletResponse response) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		String defaultValue = DefaultSysSettingKeyValue.aboutus.getValue();
		   		String byKey = sysSettingService.getByKey(DefaultSysSettingKeyValue.aboutus.getKey());
		   		if( StringHelper.isNotBlank(byKey) ){
		   			defaultValue = byKey;
		   		}
		   		resultMap.put(DATA, defaultValue);
		   		errorCode = SUCCESS_ERROR_CODE;
		       	errorMsg = SUCCESS_ERROR_MSG;
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("IndexPageInterface", "index", e.toString(),"APP欢迎页",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
			return resultMap;
		}
	    
	    /**
	    * 欢迎页
	    * @param request
	    * @param response
	    * @return
	    */
	    @RequestMapping(value = "/protocol")
		@ResponseBody
		public Map<String, Object> protocol(HttpServletRequest request, HttpServletResponse response) {
			Map<String,Object> resultMap = new HashMap<String,Object>();
			String errorCode = PARAM_ERROR_CODE;
		   	String errorMsg = PARAM_ERROR_MSG;
		   	try{
		   		String defaultValue = DefaultSysSettingKeyValue.protocol.getValue();
		   		String byKey = sysSettingService.getByKey(DefaultSysSettingKeyValue.protocol.getKey());
		   		if( StringHelper.isNotBlank(byKey) ){
		   			defaultValue = byKey;
		   		}
		   		resultMap.put(DATA, defaultValue);
		   		errorCode = SUCCESS_ERROR_CODE;
		       	errorMsg = SUCCESS_ERROR_MSG;
		   	}catch(Exception e){
		   		errorCode = SERVER_ERROR_CODE;
		       	errorMsg = SERVER_ERROR_MSG;
		   		logException("IndexPageInterface", "index", e.toString(),"APP欢迎页",e);
		   	}
		   	resultMap.put(CODE, errorCode);
		   	resultMap.put(MSG, errorMsg);
			return resultMap;
		}  
	   
	   
}